package com.ang.jdk8.stream.sort;

import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class SortMap {
    public static void main(String[] args) {
        SortMap sortMap = new SortMap();
        sortMap.test1();
        ;
    }


    public void test1() {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, 8);
        map.put(3, 6);
        map.put(1, 5);
        map.put(9, 5);
        map.put(2, 3);
        map.put(4, 7);
        map.put(7, 2);
        map.put(5, 8);
        map.put(6, 3);
        map.put(8, 6);

        System.out.println("未排序--------------------");
        map.entrySet().forEach(entry -> {
//            System.out.println(entry.getKey() + ":" + entry.getValue());
        });

        System.out.println("key排序--------------------");
        map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEach(entry -> {
//            System.out.println(entry.getKey() + ":" + entry.getValue());
        });

        System.out.println("key排序--------------------");
        map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> {
//            System.out.println(entry.getKey() + ":" + entry.getValue());
        });

        System.out.println("value排序--------------------");
        map.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach(entry -> {
//            System.out.println(entry.getKey() + ":" + entry.getValue());
        });

        //先value，在key
        System.out.println("先value，在key--------------------");
        Map<Integer, Integer> collect = new LinkedHashMap<>();
        map.entrySet().stream()
                .sorted(Map.Entry.comparingByValue())
                .forEachOrdered(entry -> collect.put(entry.getKey(), entry.getValue()));
        collect.entrySet().stream().forEach(entry -> {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        });
    }
}
